package com.abbas.abchat.dao.impl;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.text.TextUtils;

import com.abbas.abchat.AbChatApplication;
import com.abbas.abchat.dao.AccountDao;
import com.abbas.abchat.db.Sqlite;
import com.abbas.abchat.entity.Account;
import com.abbas.abchat.entity.ChatTable;

import java.util.ArrayList;
import java.util.List;

/**
 * @author abbas
 * @description: TODO
 * @date 2022/4/13 14:24
 */
public class AccountDaoImpl implements AccountDao {

    private SQLiteDatabase sqLiteDatabase;


    public AccountDaoImpl(Context context) {
        this.sqLiteDatabase = new Sqlite(context, Sqlite.DB_NAME, null, 1).getWritableDatabase();
    }

    @Override
    public List<Account> findAll() throws Exception {
        throw new IllegalStateException("未编写方法");
    }

    @SuppressLint("Range")
    @Override
    public Account findById(int _id) throws Exception {
        Cursor cursor = sqLiteDatabase.query(Account.DATABASE_NAME, null, "_id = ?", new String[]{_id + ""}, null, null, null);
        Account account = new Account();
        if (cursor.moveToNext()) {
            account.setId(cursor.getInt(cursor.getColumnIndex(Account.ID)));
            account.setAccount(cursor.getString(cursor.getColumnIndex(Account.ACCOUNT)));
            account.setPassword(cursor.getString(cursor.getColumnIndex(Account.PASSWORD)));
            account.setName(cursor.getString(cursor.getColumnIndex(Account.NAME)));
            account.setBalance(cursor.getDouble(cursor.getColumnIndex(Account.BALANCE)));
            account.setChatTableDbName(cursor.getString(cursor.getColumnIndex(Account.CHAT_TABLE_DBNAME)));
            account.setImgPath(cursor.getString(cursor.getColumnIndex(Account.IMAGE_PATH)));
        }
        cursor.close();
        return account;
    }

    @Override
    public long insert(Account entity) throws Exception {
        ContentValues values = new ContentValues();
        if (!TextUtils.isEmpty(entity.getAccount())) {
            values.put(Account.ACCOUNT, entity.getAccount());
        }
        if (!TextUtils.isEmpty(entity.getPassword())) {
            values.put(Account.PASSWORD, entity.getPassword());
        }
        if (!TextUtils.isEmpty(entity.getName())) {
            values.put(Account.NAME, entity.getName());
        }
        if (!TextUtils.isEmpty(String.valueOf(entity.getBalance()))) {
            values.put(Account.BALANCE, entity.getBalance());
        }
        if (!TextUtils.isEmpty(entity.getChatTableDbName())) {
            values.put(Account.CHAT_TABLE_DBNAME, entity.getChatTableDbName());
        }
        if (!TextUtils.isEmpty(entity.getImgPath())) {
            values.put(Account.IMAGE_PATH, entity.getImgPath());
        }
        return sqLiteDatabase.insert(Account.DATABASE_NAME, null, values);
    }

    @Override
    public int deleteById(int _id) throws Exception {
        return 0;
    }

    @Override
    public int updateById(Account entity) throws Exception {
        ContentValues values = new ContentValues();
        if (!TextUtils.isEmpty(entity.getAccount())) {
            values.put(Account.ACCOUNT, entity.getAccount());
        }
        if (!TextUtils.isEmpty(entity.getPassword())) {
            values.put(Account.PASSWORD, entity.getPassword());
        }
        if (!TextUtils.isEmpty(entity.getName())) {
            values.put(Account.NAME, entity.getName());
        }
        if (!TextUtils.isEmpty(String.valueOf(entity.getBalance()))) {
            values.put(Account.BALANCE, entity.getBalance());
        }
        if (!TextUtils.isEmpty(entity.getChatTableDbName())) {
            values.put(Account.CHAT_TABLE_DBNAME, entity.getChatTableDbName());
        }
        if (!TextUtils.isEmpty(entity.getImgPath())) {
            values.put(Account.IMAGE_PATH, entity.getImgPath());
        }
        return sqLiteDatabase.update(Account.DATABASE_NAME, values, "_id = ?", new String[]{entity.getId() + ""});
    }


    @SuppressLint("Range")
    @Override
    public Account findByAccount(String targetAccount) throws Exception {
        Cursor cursor = sqLiteDatabase.query(Account.DATABASE_NAME, null, "account = ?", new String[]{targetAccount}, null, null, null);
        Account account = null;
        if (cursor.moveToNext()) {
            account = new Account();
            account.setId(cursor.getInt(cursor.getColumnIndex(Account.ID)));
            account.setAccount(cursor.getString(cursor.getColumnIndex(Account.ACCOUNT)));
            account.setPassword(cursor.getString(cursor.getColumnIndex(Account.PASSWORD)));
            account.setName(cursor.getString(cursor.getColumnIndex(Account.NAME)));
            account.setBalance(cursor.getDouble(cursor.getColumnIndex(Account.BALANCE)));
            account.setChatTableDbName(cursor.getString(cursor.getColumnIndex(Account.CHAT_TABLE_DBNAME)));
            account.setImgPath(cursor.getString(cursor.getColumnIndex(Account.IMAGE_PATH)));
        }
        cursor.close();
        return account;
    }

    /**
     * 未完成
     * @param ids
     * @return
     * @throws Exception
     */
    @SuppressLint("Range")
    @Override
    public List<Account> findListById(Integer[] ids) throws Exception {
//        StringBuilder stringBuilder = new StringBuilder("");
//        List<Account> accounts = new ArrayList<>();
//        for (int i = 0; i < ids.length; i++) {
//            if (i != ids.length - 1){
//                stringBuilder.append(ids[i]).append(",");
//            }else {
//                stringBuilder.append(ids[i]);
//            }
//        }
//        Cursor cursor = sqLiteDatabase.rawQuery("select * from " + Account.DATABASE_NAME + " where " + Account.ID + " in (?)", new String[]{stringBuilder.toString()});
//        if (cursor.moveToNext()) {
//            Account account = new Account();
//            account.setId(cursor.getInt(cursor.getColumnIndex(Account.ID)));
//            account.setAccount(cursor.getString(cursor.getColumnIndex(Account.ACCOUNT)));
//            account.setPassword(cursor.getString(cursor.getColumnIndex(Account.PASSWORD)));
//            account.setName(cursor.getString(cursor.getColumnIndex(Account.NAME)));
//            account.setBalance(cursor.getDouble(cursor.getColumnIndex(Account.BALANCE)));
//            account.setChatTableDbName(cursor.getString(cursor.getColumnIndex(Account.CHAT_TABLE_DBNAME)));
//            account.setImgPath(cursor.getString(cursor.getColumnIndex(Account.IMAGE_PATH)));
//            accounts.add(account);
//        }
//        cursor.close();

        return null;
    }


    @SuppressLint("Range")
    @Override
    public List<Account> findByNickName(String targetStr) throws Exception {
        Cursor cursor = sqLiteDatabase.query(Account.DATABASE_NAME, null, Account.NAME + " like ?", new String[]{"%" + targetStr + "%"}, null, null, null);
        List<Account> accounts = new ArrayList<>();
        while (cursor.moveToNext()) {
            Account account = new Account();
            account.setId(cursor.getInt(cursor.getColumnIndex(Account.ID)));
            account.setAccount(cursor.getString(cursor.getColumnIndex(Account.ACCOUNT)));
            account.setPassword(cursor.getString(cursor.getColumnIndex(Account.PASSWORD)));
            account.setName(cursor.getString(cursor.getColumnIndex(Account.NAME)));
            account.setBalance(cursor.getDouble(cursor.getColumnIndex(Account.BALANCE)));
            account.setChatTableDbName(cursor.getString(cursor.getColumnIndex(Account.CHAT_TABLE_DBNAME)));
            account.setImgPath(cursor.getString(cursor.getColumnIndex(Account.IMAGE_PATH)));
            accounts.add(account);
        }
        cursor.close();
        return accounts;
    }

    @SuppressLint("Range")
    @Override
    public List<Account> findListById(List<ChatTable> chatTables) throws Exception {
//        StringBuilder selectionArgs = new StringBuilder("(");
//        for (int i = 0; i < chatTables.size(); i++) {
//            if (i != chatTables.size()-1){
//                selectionArgs.append(chatTables.get(i).getPeopleId()).append(",");
//            }else {
//                selectionArgs.append(chatTables.get(i).getPeopleId()).append(")");
//            }
//        }
//        Cursor cursor = sqLiteDatabase.query(Account.DATABASE_NAME, null, "_id = ?", new String[]{selectionArgs.toString()}, null, null, null);
//        List<Account> accounts = new ArrayList<>();
//        while (cursor.moveToNext()){
//            Account account = new Account();
//            account.set_id(cursor.getInt(cursor.getColumnIndex(Account.ID)));
//            account.setAccount(cursor.getString(cursor.getColumnIndex(Account.ACCOUNT)));
//            account.setPassword(cursor.getString(cursor.getColumnIndex(Account.PASSWORD)));
//            account.setName(cursor.getString(cursor.getColumnIndex(Account.NAME)));
//            account.setChatTableDbName(cursor.getString(cursor.getColumnIndex(Account.CHATTABLEDBNAME)));
//            account.setImgPath(cursor.getString(cursor.getColumnIndex(Account.IMAGE_PATH)));
//            accounts.add(account);
//        }
//        cursor.close();
        List<Account> accounts = new ArrayList<>();
        for (ChatTable chatTable : chatTables) {
            Cursor cursor = sqLiteDatabase.query(Account.DATABASE_NAME, null, "_id = ?", new String[]{chatTable.getPeopleId() + ""}, null, null, null);
            while (cursor.moveToNext()) {
                Account account = new Account();
                account.setId(cursor.getInt(cursor.getColumnIndex(Account.ID)));
                account.setAccount(cursor.getString(cursor.getColumnIndex(Account.ACCOUNT)));
                account.setPassword(cursor.getString(cursor.getColumnIndex(Account.PASSWORD)));
                account.setName(cursor.getString(cursor.getColumnIndex(Account.NAME)));
                account.setBalance(cursor.getDouble(cursor.getColumnIndex(Account.BALANCE)));
                account.setChatTableDbName(cursor.getString(cursor.getColumnIndex(Account.CHAT_TABLE_DBNAME)));
                account.setImgPath(cursor.getString(cursor.getColumnIndex(Account.IMAGE_PATH)));
                accounts.add(account);
            }
            cursor.close();
        }
        return accounts;
    }

    @Override
    public int deleteByAccount(String account) throws Exception {
        return 0;
    }

    @Override
    public int updateByAccount(String account) throws Exception {
        return 0;
    }

}
